﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;
using MvcProject.Utils;

namespace MvcProject.Controllers
{
    public class ContractInfoController : Controller
    {
        private ContractInfoModel db = new ContractInfoModel();
        //
        // GET: /ContractInfo/

        public ActionResult Index(int? projectId)
        {
            ViewData["projectinfos"] = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            int _projectId = projectId ?? 0;
            if (_projectId != 0)
            {
                var contracts = db.FindAllContractInfosByForeignerId(_projectId);
                return View(contracts);
            }
            else
            {
                var contracts = db.FindAllContractInfos();
                return View(contracts);
            }
        }
        public ActionResult ProjectContractIndex(int? projectId)
        {
            //获取所有的项目ID
            ViewData["projectinfos"] = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            int _projectId = projectId ?? 0;
            if (_projectId != 0)//不为0，表示要获取指定项目下的信息
            {
                var contracts = db.FindProjectContractInfosByForeignerId(_projectId);
                return View(contracts);
            }
            else//为0，表示要获取所有项目下的信息
            {
                var contracts = db.FindProjectContractInfos();
                return View(contracts);
            }
        }
        public ActionResult TeamContractIndex(int? projectId, int? teamId)
        {
            //获取所有的项目ID
            ViewData["projectinfos"] = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            int _teamId = teamId ?? 0;
            int _projectId = projectId ?? 0;
            //通过项目ID获取班组ID
            ViewData["teams"] = new SelectList(new BaseModels().FindAllTeamsByProjectId(_projectId), "teamid", "teamname");
            if (_teamId != 0)//不为0，表示要获取指定班组下的信息
            {
                var contracts = db.FindTeamContractInfosByForeignerId(_teamId);
                return View(contracts);
            }
            else//为0，表示要获取所有班组下的信息
            {
                var contracts = db.FindTeamContractInfos();
                return View(contracts);
            }
        }
        /// <summary>
        /// 通过项目ID获取班组ID
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public JsonResult GetTeamsByProjectId(int id)
        {
            BaseModels db = new BaseModels();
            var teams = db.FindAllTeamsByProjectId(id);
            return Json(teams.ToList(), JsonRequestBehavior.AllowGet); 
        }
        //
        // GET: /ContractInfo/Details/5

        public ActionResult Details(int id)
        {
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos = new BaseModels().GetAttachInfos(AttachinfoEnum.Contract.ToString(), id);
            ViewData["attachinfos"] = attachinfos;
            ContractInfo dinner = db.GetContractInfo(id);
            return View(dinner);
        }

        //
        // GET: /ContractInfo/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ContractInfo/Create

        [HttpPost]
        public ActionResult Create(int foreignerId, int type, ContractInfo dinner)
        {
            if (ModelState.IsValid)
                try
                {
                    UpdateModel(dinner);
                    dinner.ForeignerId = foreignerId;
                    dinner.ContractType = type;
                    dinner.CreateDate = System.DateTime.Now;
                    dinner.CreateUserName = "ywx";
                    db.Add(dinner);
                    db.Save();
                    return RedirectToAction("Details", new { id = dinner.ContractId });
                }
                catch (Exception e)
                {
                    return View(dinner);
                }
            return View(dinner);
        }
        
        //
        // GET: /ContractInfo/Edit/5
 
        public ActionResult Edit(int id)
        {
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos = new BaseModels().GetAttachInfos(AttachinfoEnum.Contract.ToString(), id);
            ViewData["attachinfos"] = attachinfos;
            ContractInfo dinner = db.GetContractInfo(id);
            return View(dinner);
        }

        //
        // POST: /ContractInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            ContractInfo dinner = db.GetContractInfo(id);
            UpdateModel(dinner);
            dinner.CreateDate = System.DateTime.Now;
            dinner.CreateUserName = "ywx";
            db.Save();
            return RedirectToAction("Details", new { id = dinner.ContractId });
        }

        //
        // GET: /ContractInfo/Delete/5
 
        public ActionResult Delete(int id,int? type)
        {
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos = new BaseModels().GetAttachInfos(AttachinfoEnum.Contract.ToString(), id);
            ViewData["attachinfos"] = attachinfos;
            ContractInfo dinner = db.GetContractInfo(id);
            return View(dinner);
        }
        //
        // POST: /ContractInfo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            ViewData["projectinfos"] = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            ContractInfo dinner = db.GetContractInfo(id);
            if (dinner == null)
                return View("../ProjectInfo/NotFound");
            db.Delete(dinner);
            db.Save();
            BaseModels bm = new BaseModels();
            bm.DeleteAttachInfo(id, AttachinfoEnum.Contract.ToString());
            return View("Index");
        }
    }
}
